package com.applet.system.service.impl;

import java.util.List;


import com.applet.code.exception.BusinessRuntimeException;
import com.applet.code.exception.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.applet.system.entity.User;
import com.applet.system.entity.dto.UserDto;
import com.applet.system.mapper.UserMapper;
import com.applet.system.service.UserService;

import javax.annotation.Resource;


@Service
public class UserServiceImpl implements UserService{

	@Resource
	private UserMapper userMapper;
	
	@Autowired
	private StringRedisTemplate stringRedisTemplate;
	
	@Override
	public User getUserByPassword(User user) {
		user.setPassword(this.salt(user.getUserName(), user.getPassword()));
		return this.userMapper.getUserByPassword(user);
	}

	@Override
	public void updateSelfInformation(User user) {
		this.userMapper.updateSelfInformation(user);
	}

	@Override
	public int updateSelfPassword(UserDto user) {
		user.setNewPassword(this.salt(user.getUserName(), user.getNewPassword()));
		user.setPassword(this.salt(user.getUserName(), user.getPassword()));
		return this.userMapper.updateSelfPassword(user);
	}

	@Override
	public boolean getRoleIsUsed(Long roleId) {
		return this.userMapper.getRoleIsUsed(roleId);
	}

	@Override
	public List<Long> getUserIdsByRoleId(Long roleId) {
		return this.userMapper.getUserIdsByRoleId(roleId);
	}

	@Override
	public List<UserDto> getPage(UserDto user) {
		return this.userMapper.getPage(user);
	}

	@Override
	@Transactional
	public void updateUserStatus(User user) {
		int row = this.userMapper.updateUserStatus(user);
//		if (row == 0) {
//			throw new BusinessException("当前用户已经处于{}状态,无须修改", user.getIsEnable() ? "启用" : "禁用");
//		}
	}

	@Override
	public void resetPwd(User user) {
		String userName = this.userMapper.getUserName(user.getId());
		if(userName == null) {
			throw new BusinessRuntimeException(ResultCode.BUSINESS_ERROR);
		}
		user.setPassword(this.salt(userName, user.getPassword()));
		this.userMapper.updatePwd(user);
	}

	@Override
	public void insert(User user) {
		if (this.userMapper.getUserNameIsUsed(user.getUserName())) {
			throw new BusinessRuntimeException(ResultCode.BUSINESS_ERROR);
		}
		user.setPassword(this.salt(user.getUserName(), user.getPassword()));
		this.userMapper.insert(user);
	}

	@Override
	public void settingRoles(UserDto user) {
		this.userMapper.deleteUserRoles(user.getId());
		if (user.getRoleIds() != null && !user.getRoleIds().isEmpty()) {
			this.userMapper.insertUserRoles(user);
		}
		String url_key = "console:user:resource:url:" + user.getId();
    	String code_key = "console:user:resource:code:" + user.getId();
    	this.stringRedisTemplate.delete(url_key);
    	this.stringRedisTemplate.delete(code_key);
	}

	@Override
	public List<Long> getUserRoleIds(Long userId) {
		return this.userMapper.getUserRoleIds(userId);
	}



}
